﻿using Gx.Common;
using Gx.Common.Core;
using Gx.Dal;
using Gx.Dal.Manage;
using Gx.Dal.Shared;
using Gx.Model;
using Gx.Model.Entity;
using Gx.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;


namespace Gx.Bll.Manage
{
    public class LKSettlementPaymentBll
    {
    
        protected LKSettlementPaymentDal dal = new LKSettlementPaymentDal();
        protected BaseDal<LKSettlementPaymentDetail> detailDal = new BaseDal<LKSettlementPaymentDetail>();
        protected BaseDal<LKCustomerPrePayment> customerPrePayDal = new BaseDal<LKCustomerPrePayment>();

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public PagedList<LKSettlementPayment> GetLKSettlementPaymentPageList(LKSettlementPaymentInput input)
        {
            return dal.GetLKSettlementPaymentPageList(input);
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public List<LKSettlementPayment> GetLKSettlementPayments()
        {
            return dal.GetAll().ToList();
        }

        /// <summary>
        /// 新增结算收款单
        /// </summary>
        /// <param name="lKSettlementPayment"></param>
        /// <returns></returns>
        public ReturnModel AddLKSettlementPayment(LKSettlementPaymentDto lKSettlementPaymentDto)
        {
            

            string maxCode = dal.GetMaxLKSettlementPaymentCode();
            string code = "S" + DateTime.Now.ToString("yyMMdd") + "001";
            if (!string.IsNullOrWhiteSpace(maxCode))
            {
                //获取240716023月份，07为月份
                var maxMonth = maxCode.Substring(2, 2);
                var nowMonth = DateTime.Now.ToString("MM");
                if (maxMonth == nowMonth)
                {
                    int num = Convert.ToInt32(maxCode);//Convert.ToInt32(maxCode.Substring(7, 3));
                    num++;
                    code = "S" + num.ToString();//DateTime.Now.ToString("yyMMdd") + num.ToString().PadLeft(3, '0');
                }

            }
            lKSettlementPaymentDto.SettlementPayment.BillNo = code;
            lKSettlementPaymentDto.SettlementPayment.CreateTime = DateTime.Now;
            lKSettlementPaymentDto.SettlementPayment.CreateUserId = LoginHelper.GetUserId();
            lKSettlementPaymentDto.SettlementPayment.CreateUser = LoginHelper.UserName();
            lKSettlementPaymentDto.SettlementPayment.CompanyId = UserContext.CompanyId;

            //获取客户余额
            var customerPrePay = customerPrePayDal.GetListByWhere("CustomerId=@CustomerId", new { lKSettlementPaymentDto.SettlementPayment.CustomerId }).Sum(x => x.PreAmount);
            //var customerPrePay = customerPre == null ? 0 : customerPre.PreAmount;

            //获取客户结算总额
            var totalAmount = dal.GetListByWhere("CustomerId=@CustomerId", new { lKSettlementPaymentDto.SettlementPayment.CustomerId }).Sum(x => x.PrePay);

            lKSettlementPaymentDto.SettlementPayment.PreAmount = customerPrePay - lKSettlementPaymentDto.SettlementPayment.PrePay - totalAmount;

            var result= dal.AddLKSettlementPayment(lKSettlementPaymentDto);
            return new ReturnModel { Success = result, Msg = result ? "新增成功" : "新增失败" };
        }

        /// <summary>
        /// 编辑结算收款单
        /// </summary>
        /// <param name="lKSettlementPayment"></param>
        /// <returns></returns>
        public bool EditLKSettlementPayment(LKSettlementPaymentDto lKSettlementPaymentDto)
        {
            var lKSettlementPayment = lKSettlementPaymentDto.SettlementPayment;
            var cus = dal.Get(lKSettlementPayment.Id);
            if (cus == null)
            {
                return false;
            }
            if (cus.CompanyId != UserContext.CompanyId) return false;
            lKSettlementPayment.BillNo = cus.BillNo;
            lKSettlementPayment.CreateTime = cus.CreateTime;
            lKSettlementPayment.CreateUserId = cus.CreateUserId;
            lKSettlementPayment.CreateUser = cus.CreateUser;
            lKSettlementPayment.ModifyUserId = LoginHelper.GetUserId();
            lKSettlementPayment.ModifyUser = LoginHelper.UserName();
            lKSettlementPayment.ModifyTime = DateTime.Now;
            lKSettlementPayment.CompanyId = UserContext.CompanyId;

            //获取客户余额
            var customerPrePay = customerPrePayDal.GetListByWhere("CustomerId=@CustomerId", new { lKSettlementPaymentDto.SettlementPayment.CustomerId }).Sum(x => x.PreAmount);
            //var customerPrePay = customerPre == null ? 0 : customerPre.PreAmount;


            //获取客户结算总额
            var totalAmount = dal.GetListByWhere("CustomerId=@CustomerId AND Id !=@Id", new { lKSettlementPaymentDto.SettlementPayment.CustomerId, lKSettlementPaymentDto.SettlementPayment.Id }).Sum(x => x.PrePay);

            lKSettlementPaymentDto.SettlementPayment.PreAmount = customerPrePay - totalAmount - lKSettlementPaymentDto.SettlementPayment.PrePay;

            return dal.EditLKSettlementPayment(lKSettlementPaymentDto);
        }

        public decimal GetPreAmount(int customerId)
        {
            var customerPre = customerPrePayDal.GetListByWhere("CustomerId=@customerId", new { customerId }).Sum(x => x.PreAmount);

            //获取客户结算总额
            var totalAmount = dal.GetListByWhere("CustomerId=@customerId", new { customerId }).Sum(x => x.PrePay);
            return customerPre - totalAmount;
        }

        /// <summary>
        /// 删除结算收款单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ReturnModel DeleteLKSettlementPayment(int id)
        {
            var bill = dal.Get(id);
            if (bill == null) return new ReturnModel { Success = false, Msg = "结算收款单不存在" };
            if (bill.CompanyId != UserContext.CompanyId) return new ReturnModel { Success = false, Msg = "无权限操作" };
            if(bill.IsReview) return new ReturnModel { Success = false, Msg = "已复核的结算收款单不能删除" };
            var result= dal.DeleteLKSettlementPayment(id);

            return new ReturnModel { Success = result, Msg = result ? "删除成功" : "删除失败" };
        }

        /// <summary>
        /// 获取结算收款单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public LKSettlementPaymentDto GetLKSettlementPaymentById(int id)
        {
            var bill = dal.Get(id);
            if (bill.CompanyId != UserContext.CompanyId)
                return new LKSettlementPaymentDto();
            var details= detailDal.GetListByWhere("SettlementId=@SettlementId", new { SettlementId = id }).ToList();
            return new LKSettlementPaymentDto { SettlementPayment = bill, Details = details };
        }

        //获取客户待收费明细
        public List<LKSettlementPaymentDetail> GetLKSettlementPaymentDetailListByCustomer(SelectCostInput input)
        {
            return dal.GetLKSettlementPaymentDetailListByCustomer(input);
        }

        public LKSettlementPaymentDetail GetLKSettlementPaymentDetailListByGoodsNo(string goodsNo, int customerId) 
        {
            return dal.GetLKSettlementPaymentDetailListByGoodsNo(goodsNo, customerId);
        }

        //复核
        public bool ReviewLKSettlementPayment(int id)
        {
            var bill = dal.Get(id);
            if (bill == null) return false;
            if (bill.CompanyId != UserContext.CompanyId) return false;
            bill.IsReview = true;
            bill.ReviewerId = LoginHelper.GetUserId();
            bill.Reviewer = LoginHelper.UserName();
            bill.ReviewTime = DateTime.Now;

            return dal.Update(bill);
        }
    }
}

